package com.topnav.mc.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.lib.mybitsplus.JsController;
import com.js.lib.mybitsplus.page.JsPage;
import com.js.lib.mybitsplus.page.ResultPage;
import com.js.lib.result.ApiResult;
import com.js.lib.result.Result;
import com.topnav.mc.admin.entity.DeviceAlarm;
import com.topnav.mc.admin.entity.bean.DeviceAlarmBean;
import com.topnav.mc.admin.entity.enums.DeviceAlarmCate;
import com.topnav.mc.admin.entity.enums.EnumModal;
import com.topnav.mc.admin.entity.param.DeviceAlarmParam;
import com.topnav.mc.admin.entity.wrapper.DeviceAlarmWrapper;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.admin.service.IDeviceAlarmService;
import com.topnav.mc.base.mc.McController;
import com.topnav.mc.base.mc.McPage;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.PermissionGradeUtil;
import com.topnav.mc.uitls.ResultErrorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: kenny
 * @Date: 2023/11/7 11:50
 */
@Api(value = "admin/device/alarm", tags = "设备报警管理")
@RestController
@CrossOrigin
@RequestMapping("admin/device/alarm")
public class DeviceAlarmController extends McController<DeviceAlarm, DeviceAlarmBean, DeviceAlarmParam, IDeviceAlarmService, DeviceAlarmWrapper> {
    @Autowired
    private IDeviceAlarmService deviceAlarmService;
    @Autowired
    private PermissionGradeUtil permissionGradeUtil;

    /**
     * 按报警类型统计
     * @param page
     * @return
     */
    @PostMapping({"/statistics/group/by/cate/page"})
    @ResponseBody
    public ApiResult<Page<DeviceAlarmBean>> countGroupByCatePage(@RequestBody McPage<DeviceAlarmParam> page){
        Page<DeviceAlarmBean> page1= deviceAlarmService.countGroupByCatePage(
                page.getPage(),
                this.getNewWrapper(page.getWhere()),
                page.getWhere().getAlarmCate()
                );
        return (ApiResult<Page<DeviceAlarmBean>>) ApiResult.ok(page1);
    }

    /**
     * 查询报警类型
     * @return
     */
    @GetMapping({"/query/alarm/cate"})
    @ResponseBody
    public ApiResult<List<EnumModal>> getDeviceAlarmCate() {

        return ( ApiResult<List<EnumModal>>) ApiResult.ok(DeviceAlarmCate.toList());
    }


    /**
     * 报警处置
     * @return
     */
    @PostMapping({"/dispose"})
    @ResponseBody
    public Result dispose(@RequestBody DeviceAlarm deviceAlarm) {
        PermissionGrade permissionGrade = permissionGradeUtil.getPmGrade();
        if (permissionGrade == null) {
            return ResultErrorUtil.byExceptionMsg(ExceptionMsg.ERROR_AUTHENTICATION_EFFE);
        }
        UpdateWrapper<DeviceAlarm> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("dispose",1).set("dispose_by",permissionGrade.getUserName())
                .set("dispose_user_id",permissionGrade.getUserId())
                .set("dispose_remark",deviceAlarm.getDisposeRemark())
                .set("dispose_time",new Date()).set("update_time",new Date())
                .eq("id",deviceAlarm.getId());
        if (deviceAlarmService.update(updateWrapper)){
            return Result.ok("处置完成");
        }else{
            return Result.ok("处置失败");
        }
    }



}
